﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shumisen.Core.Composition.Aop;

namespace Shumisen.Core.UnitTest.Composition.Aop
{
	[TestClass]
	public class AopCompositionContainerTest
	{

		[TestMethod]
		public void Test()
		{
			var builder = new StringBuilder();
			var aspects = (new List<Aspect>
			               {
			               	new Aspect(new MyInterceptor("1", builder), export => true),
			               	new Aspect(new MyInterceptor("2", builder), export => false),
			               	new Aspect(new MyInterceptor("3", builder), export => true),
			               });
			var container = new AopCompositionContainer(aspects, new TypeCatalog(typeof(Hoge)));
			var hoge = container.GetExportedValue<IHoge>();
			Assert.AreEqual(0, builder.Length);
			hoge.Foo();
			Assert.AreEqual("13", builder.ToString());
		}


		public class MyInterceptor : IInterceptor
		{
			private readonly string _key;

			private readonly StringBuilder _builder;

			public MyInterceptor(String key, StringBuilder builder)
			{
				_key = key;
				_builder = builder;
			}

			public void Intercept(IInvocation invocation)
			{
				_builder.Append(_key);
				invocation.Proceed();
			}
		}

		public interface IHoge
		{
			void Foo();
		}

		[Export(typeof(IHoge))]
		public class Hoge : IHoge
		{
			public void Foo()
			{
			}
		}
	}
}